package com.copa.l1669;

/**
 * @author copa
 * @createDate 2023-01-30 09:09
 * @function 双指针标记法
 */
public class Test1 {

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        int temp = 0;
        // 头指针，返回结果
        ListNode result = list1;
        // head标记合并到的头指针，tail标记合并到的尾指针
        ListNode head = null, tail = null;
        while (list1 != null) {
            if (temp == a - 1) {
                head = list1;
            }
            if (temp == b + 1) {
                tail = list1;
                break;
            }
            list1 = list1.next;
            temp++;
        }
        // 插入list2
        head.next = list2;
        while (list2.next != null) {
            list2 = list2.next;
        }
        // 插入到list1
        list2.next = tail;
        return result;
    }

    public static void main(String[] args) {
        ListNode list1 = new ListNode(0);
        list1.next = new ListNode(1);
        list1.next.next = new ListNode(2);
        list1.next.next.next = new ListNode(3);
        list1.next.next.next.next = new ListNode(4);
        list1.next.next.next.next.next = new ListNode(5);
        ListNode list2 = new ListNode(1000000);
        list2.next = new ListNode(1000001);
        list2.next.next = new ListNode(1000002);
        int a = 3;
        int b = 4;
        ListNode result = new Test1().mergeInBetween(list1, a, b, list2);
        while (result != null) {
            System.out.println(result.val);
            result = result.next;
        }
    }
}
